/*
 * BackupDAO.java
 *
 * Created on March 22, 2007, 12:34 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package finalintex2web;
import java.util.*;
import java.io.*;
import java.sql.*;

/**
 *
 * @author craig
 */
public class BackupDAO extends RevenueSourceDAO {
    
    
    /** Creates a new instance of BackupDAO */
    public BackupDAO() {
        super();
    }
    protected static RevenueSourceDAO instance = null;
        
    /**
     * 
     * @return 
     */
    public static synchronized RevenueSourceDAO getInstance(){
        if (instance == null) {
            instance = new BackupDAO();
        }
        return instance;
    }
    
    /**
     * 
     * @return 
     */
    public RevenueSourceBO create(){
        String id = GUID.generate();
        BackupBO backup = new  BackupBO(id);
        Cache c = Cache.getInstance();
        c.put(backup.getId(), backup);
        return backup;
    }
    
    /**
     * 
     * @param id 
     * @param conn 
     * @throws java.sql.SQLException 
     * @throws intex2.DataException 
     * @throws intex2.ConnectionPoolException 
     * @return 
     */
    public RevenueSourceBO read(String id, Connection conn) throws SQLException, DataException, ConnectionPoolException{
     if(Cache.getInstance().containsKey(id)){
            BackupBO backup = (BackupBO)Cache.getInstance().get(id);
            return backup;
            } 
                
           try{        
           PreparedStatement stmt = conn.prepareStatement("SELECT * FROM backups WHERE id=?");
           stmt.setString(1,id);
           ResultSet rs =  stmt.executeQuery();
           
           ConnectionPool.getInstance().release(conn);
            
            if (rs.next()) {
                BackupBO backup = new BackupBO(id);
                Cache.getInstance().put(id,backup);
                super.read(id);
                backup.setSize(rs.getInt("size"));
                backup.setBegindate(rs.getString("begindate"));
                backup.setEnddate(rs.getString("enddate"));
                Cache.getInstance().put(id,backup);      
                backup.setIndb(true);
                stmt.close();
                return backup;
               

            }else{
                throw new DataException("RevenueSourceBO was not found in the database.");
            }
           
            }catch (ConnectionPoolException e){
                throw new ConnectionPoolException("Could not retrieve a connection from the database", e);
            
            }catch (SQLException e){
                throw new DataException("SQL error", e);
            }
    }
    
    /**
     * 
     * @param revsrc 
     * @throws intex2.DataException 
     * @throws intex2.ConnectionPoolException 
     */
     public synchronized void save(RevenueSourceBO revsrc) throws DataException,ConnectionPoolException {
            
        try {
            Connection conn = ConnectionPool.getInstance().get();
            save(revsrc,conn);
            ConnectionPool.getInstance().release(conn);
        }catch (SQLException e) {
            //rollback
            throw new DataException("Could not retrieve record for id=" + revsrc.getId(), e);
            
            }catch (ConnectionPoolException e){
            //throw new ConnectionPoolException("Could not retrieve record for id=" + e);
                e.printStackTrace();
        }
        
        // release the connection back to the pool
        return;
    }
     synchronized void save(RevenueSourceBO revsrc, Connection conn) throws SQLException, DataException, ConnectionPoolException {
        // check the dirty flag in the RevenueSourceDBO.  if it is dirty, 
        // run update or insert
        if (revsrc.isDirty()) {
            if (revsrc.isIndb()) {
               update(revsrc, conn);
            }else{
               insert(revsrc, conn);
            }
            revsrc.setDirty(false);
        Cache.getInstance().touch(revsrc.getId());
     
        }
        
    }

    private synchronized void update(RevenueSourceBO revsrc, Connection conn) throws SQLException, DataException, ConnectionPoolException{
        PreparedStatement stmt = conn.prepareStatement("UPDATE backups SET begindate = ?, enddate = ?, size = ? WHERE id=? ");
        super.save(revsrc,conn);
        BackupBO backup = (BackupBO)revsrc;
        stmt.setString(1,backup.getBegindate());
        stmt.setString(2,backup.getEnddate());
        stmt.setInt(3,backup.getSize());
        stmt.setString(4,backup.getId());
        stmt.execute();
        stmt.close();
        ConnectionPool.getInstance().release(conn);
        
    }
    private synchronized void insert(RevenueSourceBO revsrc, Connection conn) throws SQLException, DataException, ConnectionPoolException{
        PreparedStatement stmt = conn.prepareStatement("INSERT INTO backups (size,begindate,enddate,id) VALUES ( ?,?,?,?)");
        
        super.save(revsrc,conn);
        BackupBO backup = (BackupBO)revsrc;
        stmt.setDouble(1,backup.getSize());
        stmt.setString(2,backup.getBegindate());
        stmt.setString(3,backup.getEnddate());
        stmt.setString(4,backup.getId());
        stmt.execute();
        stmt.close();
        // tell the RevenueSourceDBO that it's now in the db (so we call update next time not insert)
        revsrc.setIndb(true);
        ConnectionPool.getInstance().release(conn);
        
    
}
}